package com.wyt.web;

import groovy.util.logging.Slf4j;
import org.jsoup.Jsoup;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * Arcgis服务转发控制器
 *
 * @author liukai
 */
@Slf4j
@RestController
public class ArcgisController {


    private final String token = "token=QHJB,QHJB";

    private final String ArcgisHost = "http://7.170.62.10:83/arcgis/XCCG-Service/Regionalism/MapServer";

    private final String ArcgisPart = "http://7.170.62.10:83/arcgis/XCCG-Service/PartService";

    /**
     * ----------------------------------------------------------------------------------------------------------------
     * Regionalism
     *
     * @param request
     * @param response
     * @throws IOException
     */
    @GetMapping("/arcgis/XCCG-Service/PartService")
    public void partServer(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String url = ArcgisPart + "?" + token;
        PrintWriter writer = response.getWriter();
        writer.write(post(url));
        response.setContentType("text/html;charset=UTF-8");
        writer.close();
    }

    @GetMapping("/arcgis/XCCG-Service/PartService/{params}")
    public void partF(@PathVariable String params, HttpServletRequest request, HttpServletResponse response) throws IOException {
        String url = ArcgisPart + "/" + params + "?" + request.getQueryString() + "&" + token;
        PrintWriter writer = response.getWriter();
        writer.write(post(url));
        response.setContentType("text/html;charset=UTF-8");
        writer.close();
    }

    @GetMapping("/arcgis/XCCG-Service/PartService/{one}/{two}/query")
    public void partQuery(@PathVariable(name = "one") String one, @PathVariable(name = "two") String two, HttpServletRequest request, HttpServletResponse response) throws IOException {
        String url = ArcgisPart + "/" + one + "/query" + "?" + request.getQueryString() + "&" + token;
        PrintWriter writer = response.getWriter();
        writer.write(post(url));
        response.setContentType("text/html;charset=UTF-8");
        writer.close();
    }

    /**
     * ----------------------------------------------------------------------------------------------------------------
     * Regionalism
     *
     * @param request
     * @param response
     * @throws IOException
     */
    @GetMapping("/arcgis/XCCG-Service/Regionalism/MapServer")
    public void mapServer(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String originUrl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + request.getRequestURI() + "?" + request.getQueryString();
        System.out.println(originUrl);
        String url = ArcgisHost + "?" + token;
        System.out.println("Arcgis:" + url);
        PrintWriter writer = response.getWriter();
        writer.write(post(url));
        response.setContentType("text/html;charset=UTF-8");
        writer.close();
    }


    @GetMapping("/arcgis/XCCG-Service/Regionalism/MapServer/{params}")
    public void f(@PathVariable String params, HttpServletRequest request, HttpServletResponse response) throws IOException {
        String originUrl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + request.getRequestURI() + "?" + request.getQueryString();
        System.out.println(originUrl);
        String url = ArcgisHost + "/" + params + "?" + request.getQueryString() + "&" + token;
        PrintWriter writer = response.getWriter();
        writer.write(post(url));
        response.setContentType("text/html;charset=UTF-8");
        writer.close();

    }

    @GetMapping("/arcgis/XCCG-Service/Regionalism/MapServer/{one}/{two}/query")
    public void query(@PathVariable(name = "one") String one, @PathVariable(name = "two") String two, HttpServletRequest request, HttpServletResponse response) throws IOException {
        String url = ArcgisHost + "/" + one + "/query" + "?" + request.getQueryString() + "&" + token;
        PrintWriter writer = response.getWriter();
        writer.write(post(url));
        response.setContentType("application/json;charset=UTF-8");
        writer.close();
    }


    public String post(String url) throws IOException {
        Map<String, String> header = new HashMap<>(16);
        header.put("Accept", "*/*");
        header.put("Accept-Encoding", "gzip, deflate");
        header.put("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
        header.put("Content-Type", "application/json;charset=UTF-8");
        header.put("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0");
        return Jsoup.connect(url).headers(header).ignoreContentType(true).execute().body();
    }

}
